package com.qq.demo.servlet;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.connect.RequestToken;
import com.qq.connect.RedirectToken;
import com.qq.util.ParseString;

public class ConnectServlet extends HttpServlet {

	private static final long serialVersionUID = 8114052199656694703L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    RequestToken requesttoken = new RequestToken();
        String request_token = null;
        try {
            request_token = requesttoken.getRequestToken();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        HashMap<String, String> tokens = ParseString.parseTokenString(request_token);
        String oauth_token_secret = tokens.get("oauth_token_secret");
        request.getSession().setAttribute("oauth_token_secret", oauth_token_secret);
        RedirectToken redirecttoken = new RedirectToken();
        response.sendRedirect(redirecttoken.getRedirectURL(tokens));
	}
}
